package com.rfid.transport;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;

public class SerialPortFinder {
    public class Driver {
        private String mDriverName;
        private String mDeviceRoot;
        Vector<File> mDevices;

        public Driver(String name, String root) {
            this.mDevices = null;
            this.mDriverName = name;
            this.mDeviceRoot = root;
        }

        public Vector<File> getDevices() {
            if (this.mDevices == null) {
                this.mDevices = new Vector();
                File dev = new File("/dev");
                File[] files = dev.listFiles();
                if (files == null) return null;
                for (int i = 0; i < files.length; i++)
                    if (files[i].getAbsolutePath().startsWith(this.mDeviceRoot))
                        this.mDevices.add(files[i]);
            }
            return this.mDevices;
        }

        public String getName() {
            return this.mDriverName;
        }
    }

    private Vector<Driver> mDrivers = null;

    Vector<Driver> getDrivers() throws IOException {
        if (this.mDrivers == null) {
            this.mDrivers = new Vector();
            LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
            String l;
            while ((l = r.readLine()) != null) {
                String drivername = l.substring(0, 21).trim();
                String[] w = l.split(" +");
                if (w.length >= 5 && w[w.length - 1].equals("serial"))
                    this.mDrivers.add(new Driver(drivername, w[w.length - 4]));
            }
            r.close();
        }
        return this.mDrivers;
    }

    Vector<Driver> getDriversEx() throws IOException {
        if (this.mDrivers == null) {
            this.mDrivers = new Vector();
            LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers"));
            String l;
            while ((l = r.readLine()) != null) {
                String drivername = l.substring(0, 21).trim();
                String[] w = l.split(" +");
                if (w.length >= 5 && w[w.length - 1].equals("serial"))
                    this.mDrivers.add(new Driver(drivername, w[w.length - 4]));
            }
            r.close();
        }
        return this.mDrivers;
    }

    public String[] getAllDevices() {
        Vector<String> devices = new Vector<String>();
        try {
            Iterator<Driver> itdriv = getDrivers().iterator();
            while (itdriv.hasNext()) {
                Driver driver = (Driver) itdriv.next();
                Vector<File> veDev = driver.getDevices();
                Iterator<File> itdev = veDev.iterator();
                while (itdev.hasNext()) {
                    String device = ((File) itdev.next()).getName();
                    String value = String.format("%s (%s)", new Object[]{device, driver.getName()});
                    devices.add(value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (String[]) devices.toArray(new String[devices.size()]);
    }

    public String[] getAllDevicesPath() {
        Vector<String> devices = new Vector<String>();
        try {
            Iterator<Driver> itdriv = getDrivers().iterator();
            while (itdriv.hasNext()) {
                Driver driver = (Driver) itdriv.next();
                Vector<File> vectDev = driver.getDevices();
                if (vectDev == null) continue;
                Iterator<File> itdev = vectDev.iterator();
                while (itdev.hasNext()) {
                    String device = ((File) itdev.next()).getAbsolutePath();
                    File file = new File(device);
                    if (file.canRead() && file.canWrite()) devices.add(device);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (String[]) devices.toArray(new String[devices.size()]);
    }
}
